package com.dkcd.chenshiproject;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.dkcd.chenshiproject.activity.IndexActivity;
import com.dkcd.chenshiproject.entity.Song;
import com.dkcd.chenshiproject.service.MusicPlayerService;
import com.dkcd.chenshiproject.util.MusicUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public static List<Song> musicData;
    public static Messenger messenger;
    public static boolean bound;
    public static Intent intent;
    public static ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            messenger = new Messenger(iBinder);
            bound = true;
            Log.e("服务连接", "onServiceDisconnected: "+bound );
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            bound = false;
            Log.e("服务连接", "onServiceDisconnected: "+bound );

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    public void init(){
        request_permissions();
    }

    // 请求权限回调方法
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1002:
                // 1002请求码对应的是申请多个权限
                if (grantResults.length > 0) {
                    // 因为是多个权限，所以需要一个循环获取每个权限的获取情况
                    for (int i = 0; i < grantResults.length; i++) {
                        // PERMISSION_DENIED 这个值代表是没有授权，我们可以把被拒绝授权的权限显示出来
                        if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                            Toast.makeText(MainActivity.this, permissions[i] + "重要权限被拒绝，请手动开启", Toast.LENGTH_SHORT).show();
                            onDestroy();
                        }
                    }
                }
                break;
        }
        searchLocalMusic();
        //第一次申请权限结束，进行初始化
        toIndexPage();

    }


    // 请求多个权限
    private void request_permissions() {
        // 创建一个权限列表，把需要使用而没用授权的的权限存放在这里
        List<String> permissionList = new ArrayList<>();

        // 判断权限是否已经授予，没有就把该权限添加到列表中
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }

        // 如果列表为空，就是全部权限都获取了，不用再次获取了。不为空就去申请权限
        if (!permissionList.isEmpty()) {
            findViewById(R.id.app_init_progress_linearLayout).setVisibility(View.VISIBLE);
            ActivityCompat.requestPermissions(this,
                    permissionList.toArray(new String[permissionList.size()]), 1002);
        } else {
//            Toast.makeText(this, "多个权限你都有了，不用再次申请", Toast.LENGTH_LONG).show();
            searchLocalMusic();
            toIndexPage();
        }
    }

    private void toIndexPage(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(MainActivity.this, IndexActivity.class);
                startActivity(intent);
            }
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MainActivity.this.finish();
    }

    /**
     *
     * @author ChenShi
     * @version：1.0
     * @date
     * 描述: 查询本地音乐
     *
     */
    public void searchLocalMusic(){
        musicData = MusicUtils.getMusicData(this);
        intent = new Intent(this, MusicPlayerService.class);
        bindService(intent,connection,BIND_AUTO_CREATE);
    }
}